﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GlxRRSDB.ReportDataSetTableAdapters;
using GlxRRSDB;

namespace GlxRRSMain.UserControls
{
    public partial class SessionManagementControl : UserControl
    {
        private CinemaSessionViewTableAdapter x_CinemaSessionViewTableAdapter = new CinemaSessionViewTableAdapter();
        private List<ReportDataSet.CinemaSessionViewRow> x_ListCinemaSession = null;
        ReportDataSet.CinemaSessionViewRow x_CinemaSession = null;
        //
        private tblCinemaTableAdapter x_tblCinemaTableAdapter = new tblCinemaTableAdapter();
        private List<ReportDataSet.tblCinemaRow> x_ListCinema = null;
        //
        private tblSessionTableAdapter x_tblSessionTableAdapter = new tblSessionTableAdapter();
        private List<string> x_ListScreenName = new List<string>();
        //
        private void SessionManagementControl_Load(object sender, EventArgs e)
        {
            cbCinema.DataSource = ListCinema;
            cbCinema.DisplayMember = "CinemaName";
            initDataGridView();
            //
            gridSession.DataSource = ListCinemaSession;
        }
        public SessionManagementControl()
        {
            InitializeComponent();
        }
        private void initDataGridView()
        {
            initSessionDataGridView();
        }
        private void initSessionDataGridView()
        {
            gridSession.AutoGenerateColumns = false;
            gridSession.ReadOnly = true;

            DataGridViewTextBoxColumn c01 = new DataGridViewTextBoxColumn();
            c01.DataPropertyName = "SessionID";
            c01.HeaderText = "Session ID";

            DataGridViewTextBoxColumn c02 = new DataGridViewTextBoxColumn();
            c02.DataPropertyName = "CinemaName";
            c02.HeaderText = "Cinema Name";

            DataGridViewTextBoxColumn c03 = new DataGridViewTextBoxColumn();
            c03.DataPropertyName = "Screen1";
            c03.HeaderText = "Screen No.1";

            DataGridViewTextBoxColumn c04 = new DataGridViewTextBoxColumn();
            c04.DataPropertyName = "Screen2";
            c04.HeaderText = "Screen No.2";

            DataGridViewTextBoxColumn c05 = new DataGridViewTextBoxColumn();
            c05.DataPropertyName = "Screen3";
            c05.HeaderText = "Screen No.3";

            DataGridViewTextBoxColumn c06 = new DataGridViewTextBoxColumn();
            c06.DataPropertyName = "Screen4";
            c06.HeaderText = "Screen No.4";

            DataGridViewTextBoxColumn c07 = new DataGridViewTextBoxColumn();
            c07.DataPropertyName = "Screen5";
            c07.HeaderText = "Screen No.5";

            DataGridViewTextBoxColumn c08 = new DataGridViewTextBoxColumn();
            c08.DataPropertyName = "Date";
            c08.HeaderText = "Session Date";
            //
            gridSession.Columns.Add(c01);
            gridSession.Columns.Add(c02);
            gridSession.Columns.Add(c03);
            gridSession.Columns.Add(c04);
            gridSession.Columns.Add(c05);
            gridSession.Columns.Add(c06);
            gridSession.Columns.Add(c07);
            gridSession.Columns.Add(c08);
        }

        private void bindingSessionData()
        {
            if (CurrentCinemaSession != null)
            {
                txtSessionID.Text = CurrentCinemaSession.SessionID.ToString();
                cbCinema.Text = CurrentCinemaSession.CinemaName;
                dpickerDateSession.Value = CurrentCinemaSession.Date;
                int screenIdx = 0;
                if (CurrentCinemaSession.Screen1 > 0)
                {
                    screenIdx = 0;
                    // value
                    txtScreen1.Text = CurrentCinemaSession.Screen1.ToString();
                }
                else if (CurrentCinemaSession.Screen2 > 0)
                {
                    screenIdx = 1;
                    // value
                    txtScreen1.Text = CurrentCinemaSession.Screen2.ToString();
                }
                else if (CurrentCinemaSession.Screen3 > 0)
                {
                    screenIdx = 2;
                    // value
                    txtScreen1.Text = CurrentCinemaSession.Screen3.ToString();
                }
                else if (CurrentCinemaSession.Screen4 > 0)
                {
                    screenIdx = 3;
                    // value
                    txtScreen1.Text = CurrentCinemaSession.Screen4.ToString();
                }
                else if (CurrentCinemaSession.Screen5 > 0)
                {
                    screenIdx = 4;
                    // value
                    txtScreen1.Text = CurrentCinemaSession.Screen5.ToString();
                }
                //
                cbScreen.SelectedIndex = screenIdx;
            }
        }

        public List<ReportDataSet.CinemaSessionViewRow> ListCinemaSession
        {
            get
            {
                if (x_ListCinemaSession == null)
                {
                    x_ListCinemaSession = x_CinemaSessionViewTableAdapter.GetData().ToList();
                }
                return x_ListCinemaSession;
            }
            set
            {
                x_ListCinemaSession = value;
            }
        }

        public ReportDataSet.CinemaSessionViewRow CurrentCinemaSession
        {
            get
            {
                if (x_CinemaSession == null)
                {
                    if (gridSession.CurrentRow != null)
                    {
                        x_CinemaSession = ListCinemaSession[gridSession.CurrentRow.Index];
                    }
                }
                return x_CinemaSession;
            }
            set
            {
                x_CinemaSession = value;
            }
        }

        public List<ReportDataSet.tblCinemaRow> ListCinema
        {
            get
            {
                if (x_ListCinema == null)
                {
                    x_ListCinema = x_tblCinemaTableAdapter.GetData().ToList();
                }
                return x_ListCinema;
            }
            set
            {
                x_ListCinema = value;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
            {
                return;
            }
            int targetID = int.Parse(txtSessionID.Text);
            gridSession.DataSource = null;
            //
            ReportDataSet.tblSessionRow current = x_tblSessionTableAdapter.GetData()
                .Where(kvp => kvp.ID == targetID)
                .First();
            x_tblSessionTableAdapter.Delete(current.ID, current.CinemaID, current.Screen1, current.Screen2, current.Screen3, current.Screen4, current.Screen5, current.Date);

            ListCinemaSession = null;
            gridSession.DataSource = ListCinemaSession;
            MessageBox.Show("Delete succeeded.", "Delete record", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void gridSession_SelectionChanged(object sender, EventArgs e)
        {
            x_CinemaSession = null;
            bindingSessionData();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                ReportDataSet.tblCinemaRow cinema = (ReportDataSet.tblCinemaRow)cbCinema.SelectedItem;
                int CinemaID = cinema.ID;
                int screenIdx = cbScreen.SelectedIndex;
                //
                int count = 0;
                x_ListScreenName = new List<string>();
                if (cinema.Screen1 > 0) count++;
                if (cinema.Screen2 > 0) count++;
                if (cinema.Screen3 > 0) count++;
                if (cinema.Screen4 > 0) count++;
                if (cinema.Screen5 > 0) count++;
                int[] screenSession = new int[5];
                for (int i = 0; i < count; i++)
                {
                    if (i == screenIdx) screenSession[i] = int.Parse(txtScreen1.Text);
                    else screenSession[i] = 0;
                }
                //
                DateTime FromDate = dpickerDateSession.Value;
                //
                ReportDataSet.tblTargetDataTable t = new ReportDataSet.tblTargetDataTable();
                //int sessionId = int.Parse(txtSessionID.Text);
                x_tblSessionTableAdapter.Insert(CinemaID, screenSession[0], screenSession[1], screenSession[2], screenSession[3], screenSession[4], FromDate);
                ListCinemaSession = null;
                gridSession.DataSource = ListCinemaSession;
            }
            catch (Exception ex) { MessageBox.Show("Insert data fail."+ex.ToString()); }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int targetID = int.Parse(txtSessionID.Text);
            //
            ReportDataSet.tblSessionRow current = x_tblSessionTableAdapter.GetData()
                .Where(kvp => kvp.ID == targetID)
                .First();
            //
            if (current == null)
            {
                MessageBox.Show("Session ID not found.");
                return;
            }
            int CinemaID = ((ReportDataSet.tblCinemaRow)cbCinema.SelectedItem).ID;
            //
            current.CinemaID = CinemaID;
            //
            int[] screenValue = new int[5];
            screenValue[0] = screenValue[1] = screenValue[2] = screenValue[3] = screenValue[4] = 0;
            //
            int screenIdx = cbScreen.SelectedIndex;
            screenValue[screenIdx] = int.Parse(txtScreen1.Text);
            // reset
            current.Screen1 = screenValue[0];
            current.Screen2 = screenValue[1];
            current.Screen3 = screenValue[2];
            current.Screen4 = screenValue[3];
            current.Screen5 = screenValue[4];
            //
            //
            current.Date = dpickerDateSession.Value;

            x_tblSessionTableAdapter.Update(current);

            ListCinemaSession = null;
            gridSession.DataSource = ListCinemaSession;
        }

        private void initCbScreen(ReportDataSet.tblCinemaRow cinema)
        {
            int count = 0;
            x_ListScreenName = new List<string>();
            if (cinema.Screen1 > 0) count++;
            if (cinema.Screen2 > 0) count++;
            if (cinema.Screen3 > 0) count++;
            if (cinema.Screen4 > 0) count++;
            if (cinema.Screen5 > 0) count++;
            for (int i = 0; i < count; i++)
            {
                x_ListScreenName.Add("Screen " + (i + 1));
            }
            //
            cbScreen.DataSource = x_ListScreenName;
        }

        private void cbCinema_SelectedIndexChanged(object sender, EventArgs e)
        {
            initCbScreen((ReportDataSet.tblCinemaRow)cbCinema.SelectedItem);
        }
    }
}
